Xbasic

RAND Function

Syntax

Random_Number as N = RAND()

Description

Returns a random number between 0 and 1.

Discussion

RAND() returns a random number between 0 and 1.

Example

rand() -> .866262

The next time the function is evaluated, it will be a different number. You can generate random numbers between different limits. To generate a number between 0 and 100, for example, use the expression:

rand() * 100

To generate a number between -100 and + 100, use the expression:

-100 + rand() * 200

RAND() always generates the same sequence of numbers. You can get an unpredictable series by looping through the RAND() function loops times before starting to use its output. The following routine uses the current time to define loops.

loops = val( right( str( int( toseconds( time() ) ) ),3) )
? loops
= 507
for i = 1 to loops
    rand()
next
? rand()
= 0.338623046875

See Also